//
//  HBCoinController.m
//  HeartBeat
//
//  Created by karong on 2023/9/11.
//

#import "HBCoinController.h"
#import "HBGoodsModel.h"
#import "HBCoinCell.h"
#import "AppDelegate.h"
#import "HBPurchaseManager.h"
#import "HBCoinFooterView.h"

@interface HBCoinController () <HBPurchaseManagerDelegate, HBCoinFooterViewDelegate>

@property (weak, nonatomic) IBOutlet UICollectionView *listView;
@property (nonatomic, strong) NSArray * dataArray;
@property (weak, nonatomic) IBOutlet UILabel *coinsLabel;
@property (weak, nonatomic) IBOutlet UIButton *payBtn;
@property (nonatomic, assign) NSInteger selectIndex;
@end

@implementation HBCoinController

@synthesize dataArray = _dataArray;

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addObserver];
    [self layoutMainView];
    [self loadGoodsListRequest];
    [HBEvenManager event:@"coin_recharge" evenLogType:(HBEvenLogType_Show)];
}

- (void)styleDidChange {
    self.view.backgroundColor = UIColor.font_white_ffffff;
}

- (void)addObserver {
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(layoutMainView) name:HBRefreshUserInfoNotification object:nil];
}

#pragma mark - NSNotification
- (void)layoutMainView {
    self.coinsLabel.text = SF(WSLocalized(@"當前餘額:%ld書幣"), (long)appUserCenter.coins);
}

- (void)loadGoodsListRequest {
    WeakSelf
    [HBToast loading];
    [HBRequest loadGoodsListWithGoods_type:1 success:^(NSArray *array) {
        weakSelf.dataArray = array;
        [HBPurchaseManager shared].delegate = weakSelf;
        [HBPurchaseManager shared].products = array;
    } failed:^(NSURLResponse * _Nonnull response, NSError * _Nonnull error, id  _Nonnull responseObject) {

    }];
}

- (IBAction)payAction:(id)sender {
    HBGoodsModel *product = self.dataArray[self.selectIndex];
    if (product.SKProduct) {
        if (appUserCenter.isLogin) {
            [HBPurchaseManager shared].delegate = self;
            [[HBPurchaseManager shared] createOrderWithProduct:product chapter:self.chapter className:NSStringFromClass(self.class)];
        }
        else {
            [((AppDelegate*)([UIApplication sharedApplication].delegate)) initMainPage];
        }
        [HBEvenManager event:@"coin_recharge" evenLogType:(HBEvenLogType_Click) attributes:@{@"coins_id":product.ios_product_id}];
    }
}
- (IBAction)popAction:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - HBPurchaseManagerDelegate
- (void)didAppleReceiveResponse {
    [HBToast dismiss];
    self.selectIndex = 0;
    [self.listView reloadData];
}

- (void)purchaseManager:(HBPurchaseManager *)manager updateStatus:(HBPurchaseStatus)status object:(id)object {
    switch (status) {
        case HBPurchaseStatusSuccess:
        {

        }
            break;
        case HBPurchaseStatusCancel:
            [HBToast simple:WSLocalized(@"支付失败")];
            break;
        default:
            break;
    }
}
#pragma mark UICollectionViewDelegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HBCoinCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HBCoinCell" forIndexPath:indexPath];
    [cell setDataWithCoin:self.dataArray[indexPath.row] select:self.selectIndex == indexPath.row];
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGSize size = CGSizeZero;
    size.width = HBScreenWidth;
    size.height = 76;
    return size;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if([kind isEqualToString:UICollectionElementKindSectionFooter]) {
        HBCoinFooterView * footerView;
        if (!footerView) {
            footerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"HBCoinFooterView" forIndexPath:indexPath];
            footerView.delegate = self;
        }
        return footerView;
    }
    return nil;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
    CGSize size = CGSizeZero;
    size.width = HBScreenWidth;
    size.height = [HBCoinFooterView cellHeight] + 26 + 45 + 1;
    return size;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    self.selectIndex = indexPath.row;
    [self.listView reloadData];
}

#pragma mark HBCoinFooterViewDelegate
- (void)vipFooterViewWebAction:(NSString *)url title:(NSString *)title {
    [ViewManager pushWebVCWithUrl:url titleStr:title inController:self];
}

- (void)setSelectIndex:(NSInteger)selectIndex {
    _selectIndex = selectIndex;
    HBGoodsModel *goods = self.dataArray[selectIndex];
    [self.payBtn setTitle:SF(WSLocalized(@"立即購買 $%.2f"), goods.price) forState:(UIControlStateNormal)];
}
@end
